//
//  SingerHomeViewController.m
//  CCBbelefitLife
//
//  Created by 小晓 on 16/1/8.
//  Copyright © 2016年 liurg. All rights reserved.
//

#import "SingerHomeViewController.h"
#import "KMusicTableViewCell.h"
#import "MusicAllAndAloneViewController.h"

@interface SingerHomeViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(strong,nonatomic)UITableView * tableView;
@property(strong,nonatomic)NSMutableArray *dataArray;

@end

@implementation SingerHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
}

- (void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:YES];
     self.tabBarController.tabBar.hidden = YES;
    self.navigationItem.title = self.titleName;
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(backBtnAction)];    
}

- (void)initView{
    
    UIImageView *headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 174)];
    headImageView.image = [UIImage imageNamed:@"图层-42@2x.png"];
    [self.view addSubview:headImageView];
    UILabel *singerNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 100, 20)];
    singerNameLabel.text = self.titleName;
    singerNameLabel.textColor = [UIColor whiteColor];
    [headImageView addSubview:singerNameLabel];
    
    UILabel *singerCountLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 130, 100, 10)];
    singerCountLabel.text = @"单曲：150";
    singerCountLabel.font = [UIFont systemFontOfSize:14];
    singerCountLabel.textColor = [UIColor whiteColor];
    [headImageView addSubview:singerCountLabel];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 174, self.view.frame.size.width, self.view.frame.size.height-174) style:(UITableViewStylePlain)];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.allowsSelection = YES;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [self requestData];
    
}

-(void)requestData{
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    NSString*infoStr11=[NSString stringWithFormat:@"/api/_search/song/singer_name/%@",self.titleName];
    NSString *intactURL = [[MyBaseUrl stringByAppendingString:infoStr11] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
    
    //    创建请求
    manager.requestSerializer =[AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json"
                     forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                     forHTTPHeaderField:@"Authorization"];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:intactURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //        [_messageArray removeAllObjects];
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSDictionary * bodyDict = [dic objectForKey:@"body"];
        NSArray * contentArray = [bodyDict objectForKey:@"content"];
        _dataArray = [NSMutableArray array];
        for(NSDictionary * contentDict in contentArray){
            
            HotMusicMsgDataModel *upinMusic = [[HotMusicMsgDataModel alloc]init];
            upinMusic.avatar = [contentDict objectForKey:@"avatar"];
            upinMusic.name = [contentDict objectForKey:@"name"];
            NSString * str = [NSString stringWithFormat:@"%@",[contentDict objectForKey:@"size"]];
            float s = [str floatValue];
            s = s/1024/100;
            upinMusic.size = [NSString stringWithFormat:@"%d",(int)s];
            upinMusic.singerName = [contentDict objectForKey:@"songerName"];
            upinMusic.accompanyUrl = [contentDict objectForKey:@"accompanyUrl"];
            upinMusic.lyric = [contentDict objectForKey:@"lyric"];
            [upinMusic fixedString];
            
            [_dataArray addObject:upinMusic];
        }
        [_tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)backBtnAction{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KMusicTableViewCell"];
    if ( !cell ) {
        cell = [[KMusicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"KMusicTableViewCell"];
    }
    HotMusicMsgDataModel *hotModel = [self.dataArray objectAtIndex:indexPath.row];
    [(KMusicTableViewCell *)cell updataCellViewWithHeadData:hotModel superViewCtr:self];
    
    return cell;
}

#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 71;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MusicAllAndAloneViewController *musicCtr = [MusicAllAndAloneViewController new];
    [self.navigationController pushViewController:musicCtr animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
